This notebook covers:

Lesson 2A-L1 Images as Functions

Import Libraries and Dependencies

In [155]:
import cv2
import os
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

IMG = 'imgs/'
Some Helper Functions

In [26]:
def show_img(img):
    """
    Function takes an image, and shows the image using pyplot.
    The image is shown in RGB
    """
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    plt.imshow(img)
Load and Display an Image

In [111]:
img_path = os.path.join(IMG, 'sea.jpeg')
img = cv2.imread(img_path)
# What is the size of the Image?
print img.shape
# What is the class of the image?
print img.dtype
show_img(img)


(3264, 4928, 3)
uint8
Inspect Image Values

My image has three channels: Red, Green, and Blue. That's why when selecting the 50th row and 100th column, three values are returned.


In [5]:
img[50,100]


Out[5]:
array([238, 217, 196], dtype=uint8)

In [6]:
plt.plot(img[1500,:,2])


Out[6]:
[<matplotlib.lines.Line2D at 0x10d4a2a50>]

TODO: Extract a 2D slice between rows 101 to 103 and columns 201 to 203 (inclusive)


In [7]:
extraction = img[101:103,201:203]
Crop Image

In [8]:
cropped = img[1700:,300:,:]

In [9]:
show_img(cropped)



In [55]:
# Size of Cropped Image
cropped.shape


Out[55]:
(1564, 4628, 3)
Color Planes

In [36]:
img_green = img[:,:,1]


Out[36]:
(3264, 4928, 3)

In [40]:
plt.imshow(img_green, cmap='gray')


Out[40]:
<matplotlib.image.AxesImage at 0x1105f42d0>
Adding Pixels

In [48]:
# Load the Dog Image
dog_path = os.path.join(IMG, 'dog.jpeg')
dog = cv2.imread(dog_path)
#dog = cv2.resize
dog = cv2.resize(dog, (img.shape[1], img.shape[0]))
show_img(dog)



In [51]:
summed = dog/2 + img/2

In [52]:
show_img(summed)



In [57]:
summed_2 = (dog + img)

In [58]:
show_img(summed_2)


Multiply by a Scalar Demo

In [88]:
def scale(img, value):
    result = img*value
    return result

scaled_img = scale(img, 2)
print img[0,0,0], '\n'
print scaled_img[0,0,0]
show_img(img)


241 

226
Blending 2 Images

Here, I used the opencv function addWeighted, instead of manipulating the matrices directly. I couldn't make it work using numpy array manipulations. If you can, please let me know or file a pull request!


In [139]:
def blend(img_1, img_2, alpha):
    #output = alpha*img_1 + (1-alpha)*img_2
    #output = cv2.multiply(img_1, alpha)
    #output = output.astype('int8')
    #output = np.zeros((img_1.shape[0], img_2.shape[1], img_1.shape[2]))
    output = cv2.addWeighted(img_1, alpha, img_2, (1-alpha), 0)
    return output

In [142]:
blended_img = blend(img, dog, 0.25)

In [143]:
show_img(blended_img)


Generate Gaussian Noise

TODO: Try Generating Other Kinds of Random Numbers. How about a 2D Grid of Random Gaussian Values?

COULDN'T DO: Generate a 2D Gaussian Plot using Matplotlib. Plot of histogram bin counts.


In [153]:
# grid = np.randn
# print np.random.randn.__doc__
grid = np.random.randn(100,100)
Apply Gaussian Noise

In [175]:
show_img(img)



In [183]:
grid = np.random.randn(img.shape[0], img.shape[1], img.shape[2])*10
gaussian_img = (img + grid).astype('uint8')
show_img(gaussian_img)